//
//  ViewController.m
//  20202notification
//
//  Created by Clement_Gu on 16/3/16.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()<ViewController2Delegate>
@property (nonatomic,strong) UITextView *txtView;
@property (nonatomic,strong) NSString* currentTime;
@property (nonatomic,strong) NSMutableArray *mArr;
@property (nonatomic,strong) NSMutableArray *getArrTxtList;
@end
/*####20202 要求
1. 放置一个TextView
2. ViewController监听应用程序发出(UIApplication)的以下通知
* 将取消活动
* 已经进入后台
* 将进入前台
* 已经激活
记录这些事件及时间,显示在TextView中
如下:(最新的信息显示在最上方)
10:26:07 已经激活
10:26:05 将进入前台
10:25:35 将进入后台
3. 这些log信息同时需要保存的，下次打开程序重新读取，显示在textView中
4. 添加一个按钮，点按钮弹出第二个页面(模态窗口)
5. 第二个页面采用 10505 设置颜色
6. 3原色的数值信息要可以采用系统的应用程序设置进行设置
7. 加导航条，及按钮，可以返回前一个页面
8. 第一个页面，页面的背景色根据的设置的这个颜色值改变
*/


//
////获得系统时间
//NSDate *  senddate=[NSDate date];
//NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
//[dateformatter setDateFormat:@"HH:mm"];
//NSString *  locationString=[dateformatter stringFromDate:senddate];
////[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
////NSString *  morelocationString=[dateformatter stringFromDate:senddate];
@implementation ViewController
//懒加载
-(NSMutableArray *)mArr
{
    if (_mArr == nil) {
        _mArr = [NSMutableArray array];
    }
    return _mArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加控件
    UITextView *txtView = [[UITextView alloc]initWithFrame:CGRectMake(5, 70, 310, 400)];
//    txtView.text = @"显示时间和事件";
    txtView.backgroundColor = [UIColor orangeColor];
    self.txtView = txtView;
    [self.view addSubview:txtView];
    //创建跳转button
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(135, 500, 50, 40)];
    [btn setTitle:@"跳转" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(present) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showData1) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showData2) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showData3) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showData4) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    [self showTxtView];
}
//获取时间并转化成字符串
-(NSString *)getTime
{
    //获取时间
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *currentTime = [formatter stringFromDate:date];
    return currentTime;
}
//跳转事件
//加导航条
-(void)present
{
    ViewController2 *vc = [[ViewController2 alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    vc.Delegate = self;
    [self presentViewController:nav animated:YES completion:nil];

}
//实现代理
-(void)getColorred:(float)red green:(float)green blue:(float)blue
{
    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

}

//显示text
-(void)showTxtView
{
    NSMutableArray *arr = [self getData];
    //创建NSString接受数组元素
    NSMutableString *mStr = [NSMutableString string];
//    遍历反向取值
    for (long i=arr.count-1 ; i >= 0; i--) {
        [mStr appendFormat:@"\n %@",arr[i]];
    }
    self.txtView.text = mStr;

}
//保存信息
-(void)saveData
{
    //定义
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //存入数组 以关键字txtView对应数组
    [user setObject:self.mArr forKey:@"Show"];
    //同步数据
    [user synchronize];
}
//读取信息
-(NSMutableArray *)getData
{
    //定义
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //取数据
   self.getArrTxtList = [user objectForKey:@"Show"];
    //返回从UserDefault取出的数组
    return self.getArrTxtList;
}
//显示信息
-(void)showData1
{

    NSString *str = [NSString stringWithFormat:@"%@ 程序取消激活状态",[self getTime]];
    //添加到数组
    [self.mArr addObject:str];
     NSLog(@"%@",self.mArr);
    //保存数据
    [self saveData];
    [self showTxtView];
}
-(void)showData2
{
   NSString *str = [NSString stringWithFormat:@"%@ 程序进入后台",[self getTime]];
    //添加到数组
    [self.mArr addObject:str];
     NSLog(@"%@",self.mArr);
    //保存数据
    [self saveData];
    [self showTxtView];
}
-(void)showData3
{
   
    NSString *str = [NSString stringWithFormat:@"%@ 程序进入前台",[self getTime]];
    //添加到数组
    [self.mArr addObject:str];
     NSLog(@"%@",self.mArr);
    //保存数据
    [self saveData];
    [self showTxtView];
}
-(void)showData4
{
    NSString *str = [NSString stringWithFormat:@"%@ 程序已激活",[self getTime]];
    //添加到数组
    
    [self.mArr addObject:str];

    //保存数据
    [self saveData];
    [self showTxtView];
}
//销毁监听
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
